package com.stary.beetlsql.generator.config;

import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.ConnectionSource;
import org.beetl.sql.core.ConnectionSourceHelper;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.NameConversion;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.db.DB2SqlStyle;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.H2Style;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.db.OracleStyle;
import org.beetl.sql.core.db.SqlServerStyle;
import org.beetl.sql.ext.DebugInterceptor;

import com.stary.beetlsql.generator.config.querys.MysqlQuery;
import com.stary.beetlsql.generator.config.querys.OracleQuery;
import com.stary.beetlsql.generator.config.querys.SqlServerQuery;
import com.stary.beetlsql.generator.config.rules.DBDriverName;
import com.stary.beetlsql.generator.config.rules.DBType;
import com.stary.beetlsql.generator.util.StringUtils;

/**
 * <p>定义常量</p>
 * @author stary {@link stary1993@qq.com}
 * @since 2019-7-24
 */
public class DataSourceConfig {

	/**
	 * 数据库类型
	 */
	private DBType dbType = DBType.ORACLE;
	/**
	 * 驱动连接的URL
	 */
	private String url;
	/**
	 * 驱动名称
	 */
	private String driverName = DBDriverName.ORACLE.getName();
	/**
	 * 数据库连接用户名
	 */
	private String username;
	/**
	 * 数据库连接密码
	 */
	private String password;

	public DataSourceConfig() {
		super();
	}
	/**
	 * 默认oracle
	 * @param url
	 * @param username
	 * @param password
	 */
	public DataSourceConfig(String url, String username, String password) {
		super();
		this.url = url;
		this.username = username;
		this.password = password;
	}


	public DataSourceConfig(DBType dbType, String url, String username,
			String password) {
		super();
		this.dbType = dbType;
		this.url = url;
		this.username = username;
		this.password = password;
	}

	public DataSourceConfig(DBType dbType, String url, String driverName,
			String username, String password) {
		super();
		this.dbType = dbType;
		this.url = url;
		this.driverName = driverName;
		this.username = username;
		this.password = password;
	}

	public DBType getDbType() {
		if (dbType == null) {
			// 默认 ORACLE
			dbType = DBType.ORACLE;
		}
		return dbType;
	}
	public DataSourceConfig setDbType(DBType dbType) {
		this.dbType = dbType;
		return this;
	}
	public String getUrl() {
		return url;
	}
	public DataSourceConfig setUrl(String url) {
		this.url = url;
		return this;
	}
	public String getDriverName() {
		if (StringUtils.isEmpty(driverName)) {
			switch (getDbType()) {
			case MYSQL:
				driverName = DBDriverName.MYSQL.getName();
				break;
			case SQL_SERVER:
				driverName = DBDriverName.SQL_SERVER.getName();
				break;
			case DB2:
				driverName = DBDriverName.DB2.getName();
				break;
			case H2:
				driverName = DBDriverName.H2.getName();
				break;
			default:
				// 默认 ORACLE
				driverName = DBDriverName.ORACLE.getName();
				break;
			}
		}
		return driverName;
	}

	public DataSourceConfig setDriverName(String driverName) {
		this.driverName = driverName;
		return this;
	}
	public String getUsername() {
		return username;
	}
	public DataSourceConfig setUsername(String username) {
		this.username = username;
		return this;
	}
	public String getPassword() {
		return password;
	}
	public DataSourceConfig setPassword(String password) {
		this.password = password;
		return this;
	}

	public DBStyle getDbStyle() {
		DBStyle dbStyle = null;
		switch (getDbType()) {
		case MYSQL:
			dbStyle = new MySqlStyle();
			break;
		case SQL_SERVER:
			dbStyle = new SqlServerStyle();
			break;
		case DB2:
			dbStyle = new DB2SqlStyle();
			break;
		case H2:
			dbStyle = new H2Style();
			break;
		default:
			// 默认 ORACLE
			dbStyle = new OracleStyle();
			break;
		}
		return dbStyle;
	}
	/**
	 * 创建数据库管理对象
	 * @param nc 命名策略
	 * @return
	 */
	public SQLManager getSQLManager(NameConversion nc) {		
		ConnectionSource source = ConnectionSourceHelper.getSimple(driverName, url, username, password);
		return new SQLManager(getDbStyle(), 
				new ClasspathLoader(), source, nc, new Interceptor[]{new DebugInterceptor()});		
	}

	public IDbQuery getQuery() {
		IDbQuery iDbQuery = null;
		switch (getDbType()) {
		case MYSQL:
			iDbQuery = new MysqlQuery();
			break;
		case SQL_SERVER:
			iDbQuery = new SqlServerQuery();
			break;
		case DB2:

			break;
		case H2:

			break;
		default:
			// 默认 ORACLE
			iDbQuery = new OracleQuery();
			break;
		}
		return iDbQuery;
	}
}